package io.github.lamtong.osgi.server;

import io.github.lamtong.osgi.api.HelloService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 服务端 {@link BundleActivator} 实现类.
 *
 * @author Lam Tong
 */
public class ServerBundleActivator implements BundleActivator {

    private static final Logger logger = Logger.getLogger(ServerBundleActivator.class.getName());

    private ServiceRegistration<HelloService> registration;

    @Override
    public void start(BundleContext context) {
        HelloService helloService = new HelloServiceImpl();
        this.registration = context.registerService(HelloService.class, helloService, null);
        logger.log(Level.INFO, "Server Bundle start and [HelloService] implementation registered.");
    }

    @Override
    public void stop(BundleContext context) {
        this.registration.unregister();
        logger.log(Level.INFO, "Server Bundle stop and [HelloService] implementation unregistered.");
    }

}
